package com.bxd.baodan.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.DatePicker;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bxd.baodan.App;
import com.bxd.baodan.R;
import com.bxd.baodan.item.ManualAddInsuranceBean;
import com.bxd.baodan.widget.DoubleDatePickerDialog;
import com.bxd.baodan.widget.IosSheetDialog;
import com.umeng.analytics.MobclickAgent;

import java.util.Calendar;

public class ManualAdd2Activity extends BaseActivity implements View.OnClickListener {

    private TextView mTvCancel;
    private RelativeLayout mRlSave;
    private TextView mTvTime;
    private TextView mtvEndTime;


    private String startTime, endTime;
    private int start, end;
    private ManualAddInsuranceBean mBean;
    private RadioGroup mRadioGroup, rgFee;

    private String renewRemind = "1";
    private String payMethod;

    String[] jobStr = {"保至终身", "自定义时间段"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manual_add2);
        hideBar();
        initView();

        mBean = (ManualAddInsuranceBean) getIntent().getSerializableExtra("bean");
    }

    private void initView() {
        mTvCancel = getTextView(R.id.tv_cancel);
        mTvCancel.setOnClickListener(this);
        mRlSave = getRelativeLayout(R.id.rl_next);
        mRlSave.setOnClickListener(this);
        mTvTime = getTextView(R.id.tv_time);
        mTvTime.setOnClickListener(this);
        mtvEndTime = getTextView(R.id.end_time);
        mtvEndTime.setOnClickListener(this);

        mRadioGroup = (RadioGroup) findViewById(R.id.radio_group);
        rgFee = (RadioGroup) findViewById(R.id.rg_fee);


        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.radio_button_1:
                        MobclickAgent.onEvent(App.getContext(), "v6_0_0_SY_bdcp_ckxq_xycp");
                        renewRemind = "1";
                        break;
                    case R.id.radio_button_2:
                        renewRemind = "0";
                        break;
                }
            }
        });


        rgFee.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.fee_all:

                        payMethod = "1";
                        break;
                    case R.id.fee_mouth:
                        payMethod = "2";
                        break;
                    case R.id.fee_year:
                        payMethod = "3";
                        break;
                }
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancel:
                ManualAdd2Activity.this.finish();
                break;
            case R.id.tv_time:


                Calendar c = Calendar.getInstance();
                // 最后一个false表示不显示日期，如果要显示日期，最后参数可以是true或者不用输入
                new DoubleDatePickerDialog(ManualAdd2Activity.this, 0, new DoubleDatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker startDatePicker, int startYear, int startMonthOfYear, int startDayOfMonth,
                                          DatePicker endDatePicker, int endYear, int endMonthOfYear, int endDayOfMonth) {

                        String sTime = new StringBuilder().append(startYear).append("-").append(startMonthOfYear + 1 < 10 ? "0" + (startMonthOfYear + 1) : startMonthOfYear + 1)
                                .append("-").append(startDayOfMonth < 10 ? "0" + startDayOfMonth : startDayOfMonth).toString();

                        start = Integer.parseInt(sTime.replace("-", ""));


                        if (end > 0 && start > 0 && (end - start) <= 0) {
                            Toast.makeText(ManualAdd2Activity.this, "结束时间早于开始时间，请重新设置！", Toast.LENGTH_SHORT).show();
                            return;
                        }


                        if (!TextUtils.isEmpty(sTime)) {
                            startTime = sTime;
                            mTvTime.setText(sTime);
                            mTvTime.setTextColor(Color.parseColor("#333333"));
                        }
                    }
                }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), true, true, false).show();

                break;
            case R.id.end_time:

                //弹窗

                IosSheetDialog dialog = new IosSheetDialog(this).
                        builder().
                        setCancelable(false).
                        setCanceledOnTouchOutside(true);
                dialog.setSheetItems(jobStr, new IosSheetDialog.DialogItemListener() {
                    @Override
                    public void onClick(int position) {
                        switch (position) {
                            case 0:
                                endTime = new StringBuilder().append(3000).append("-").append("01")
                                        .append("-").append("01").toString();
                                mtvEndTime.setText("保至终身");
                                mtvEndTime.setTextColor(Color.parseColor("#333333"));
                                break;

                            case 1:
                                Calendar c1 = Calendar.getInstance();
                                // 最后一个false表示不显示日期，如果要显示日期，最后参数可以是true或者不用输入
                                new DoubleDatePickerDialog(ManualAdd2Activity.this, 0, new DoubleDatePickerDialog.OnDateSetListener() {

                                    @Override
                                    public void onDateSet(DatePicker startDatePicker, int startYear, int startMonthOfYear, int startDayOfMonth,
                                                          DatePicker endDatePicker, int endYear, int endMonthOfYear, int endDayOfMonth) {


                                        String eTime = new StringBuilder().append(endYear).append("-").append(endMonthOfYear + 1 < 10 ? "0" + (endMonthOfYear + 1) : endMonthOfYear + 1)
                                                .append("-").append(endDayOfMonth < 10 ? "0" + endDayOfMonth : endDayOfMonth).toString();

                                        end = Integer.parseInt(eTime.replace("-", ""));

                                        if (end > 0 && start > 0 && (end - start) <= 0) {
                                            Toast.makeText(ManualAdd2Activity.this, "结束时间早于开始时间，请重新设置！", Toast.LENGTH_SHORT).show();

                                            return;
                                        }


                                        if (!TextUtils.isEmpty(eTime)) {
                                            endTime = eTime;
                                            mtvEndTime.setText(eTime);
                                            mtvEndTime.setTextColor(Color.parseColor("#333333"));
                                        }


                                    }
                                }, c1.get(Calendar.YEAR), c1.get(Calendar.MONTH), c1.get(Calendar.DATE), true, false, true).show();


                        }
                    }
                });

                dialog.show();


                break;
            case R.id.rl_next:
                String time = getString(mTvTime);
                if (TextUtils.isEmpty(time)) {
                    showToast("请选择保险生效日期!");
                    return;
                }
                String end = getString(mtvEndTime);
                if (TextUtils.isEmpty(end)) {
                    showToast("请选择保险期间!");
                    return;
                }


                if (TextUtils.isEmpty(payMethod)) {
                    showToast("请选择是缴费方式！");
                    return;
                }

                if (TextUtils.isEmpty(renewRemind)) {
                    showToast("请选择是否需要缴费到期提醒！");
                    return;
                }


                // 保存
                if (mBean == null) {
                    showToast("数据错误，请返回上一步重试！");
                    return;
                }
                mBean.insureStartDate = startTime;
                mBean.insureEndDate = endTime;
                mBean.payMethod = payMethod;
                mBean.renewRemind = renewRemind;

                Bundle b = new Bundle();
                b.putSerializable("bean", mBean);


                openActivity(ManualAdd3Activity.class, b);

                break;
        }
    }
}
